<?php

class MonitorController extends Controller
{
	
	private $quotes = array(
			array('你要相信世界上每一个人都精明，要令人信服并喜欢和你交往,那才最重要。', '李嘉诚'),
			array('该放下时且放下，你宽容别人，其实是给自己留下来一片海阔天空。', '于丹'),
			array('免费，是世界上最贵的东西。', '马云'),
			array('没有捕捉不到的猎物，就看你有没有野心去捕;没有完成不了的事情，就看你有没有野心去做。', '狼图腾'),
			array('君志所向，一往无前，愈挫愈勇，再接再厉。', '孙中山'),
	);
	
	private function getRandomQuote()
	{
		return $this->quotes[array_rand($this->quotes, 1)];
	}
	
	function actionGetQuote()
	{
		$this->renderPartial('_quote', array(
				'quote' => $this->getRandomQuote(),
		));
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'ajaxOnly + getQuote',	//Only allow getQuote via AJAX request
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','getQuote'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'expression'=>'Yii::app()->user->isAdmin()==1',
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}
	
	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{

		$dataProvider=new CActiveDataProvider('Project', array(
				'criteria'=>array(
						'with'=>array('county'),
				),
				'pagination'=>array(
						'pageSize'=>15,
				),
		));

		$this->render('index',array(
			'dataProvider' => $dataProvider,
			'quote' => $this->getRandomQuote()
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Project the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Project::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

}
